package com.oly.cms.comment.web.servie.impl;

import java.util.Iterator;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.oly.cms.comment.web.mapper.CmsCommentMapper;
import com.oly.cms.comment.web.model.CmsComment;
import com.oly.cms.comment.web.model.CmsCommentRecord;
import com.oly.cms.comment.web.model.enums.CommentVisibleEnums;
import com.oly.cms.comment.web.model.properties.OlyCommentProperties;
import com.oly.cms.comment.web.servie.ICmsCommentService;
import com.oly.cms.common.model.properties.OlyWebConfigProetries;
import com.oly.cms.common.model.support.PageData;
import com.ruoyi.system.config.service.ISysConfigService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CmsCommentServiceImpl implements ICmsCommentService {
    @Autowired
    private CmsCommentMapper cmsCommentMapper;
    @Autowired
    private ISysConfigService configService;

    /**
     * 添加评论
     * 
     * @param commentId 评论ID
     * @return
     */
    @Override
    public CmsComment getCmsCommentById(Long commentId) {
        return cmsCommentMapper.getCmsCommentById(commentId);
    }

    /**
     * 获取评论
     * 
     * @param cmsComment 评论
     * @return
     */
    @Override
    public List<CmsComment> listCmsComment(CmsComment cmsComment) {
        return cmsCommentMapper.listCmsComment(cmsComment);
    }

    /**
     * 插入评论
     * 
     * @param cmsComment 评论
     * @return
     */
    @Override
    public int insertCmsComment(CmsComment cmsComment) {
        cmsComment.setVisible(Integer.parseInt(
                configService.selectConfigDefauleValue(OlyCommentProperties.COMMENT_CONFIG_GROUP.defaultValue(),
                        OlyCommentProperties.COMMENT_DEFAULT_VISIBLE)));
        return cmsCommentMapper.insertCmsComment(cmsComment);
    }

    @Transactional
    @Override
    public int deleteCmsCommentByIds(Long[] commentIds) {
        cmsCommentMapper.deleteCmsCommentRecordsByIds(commentIds);
        return cmsCommentMapper.deleteCmsCommentByIds(commentIds);
    }

    /**
     * 批量更新评论状态
     */
    @Override
    public int updateCmsComments(Long[] ids, Byte visible) {
        visible = visible == null ? 0 : visible;
        int vs = CommentVisibleEnums.values().length;
        return cmsCommentMapper.updateCmsComments(ids, visible);
    }

    /**
     * 检查回复记录是否已经插入
     * 
     * @param cmsCommentRecord
     * @return
     */
    @Override
    public int insertCmsCommentRecord(CmsCommentRecord cmsCommentRecord) {
        if (cmsCommentMapper.checkCmsCommentRecord(cmsCommentRecord) == 0) {
            return 0;
        }
        return cmsCommentMapper.insertCmsCommentRecord(cmsCommentRecord);
    }

    @Override
    public List<CmsCommentRecord> listCmsCommentRecord(CmsCommentRecord cmsCommentRecord) {
        return cmsCommentMapper.listCmsCommentRecord(cmsCommentRecord);
    }

    @Override
    public boolean getAllowCommentByArticleId(Long articleId) {
        return cmsCommentMapper.getAllowCommentByArticleId(articleId);
    }

    @Override
    public List<CmsComment> listCommentPageOne(Long articleId) {
        List<CmsComment> cmsCommentList = cmsCommentMapper.listCmsCommentPageOne(articleId);
        Iterator<CmsComment> itf = cmsCommentList.iterator();
        while (itf.hasNext()) {
            // 获取二级评论
            canventTwoCommentNumOne((CmsComment) itf.next());
        }
        return cmsCommentList;
    }

    @Override
    public List<CmsComment> listCommentPageTwo(Long parentId) {
        List<CmsComment> cmsCommentList = cmsCommentMapper.listCmsCommentByParentId(parentId);
        return cmsCommentList;
    }

    @Override
    public List<CmsComment> listCommentParent() {
        return cmsCommentMapper.listCommentParent();
    }

    /* 封装二级评论展示10条 */
    private void canventTwoCommentNumOne(CmsComment cmsComment) {
        PageHelper.startPage(1, 20);
        cmsComment.setChildrenPageData(
                PageData.getData(cmsCommentMapper.listCmsCommentByParentId(cmsComment.getCommentId())));
    }

    // 获取最新留言列表
    @Override
    public List<CmsComment> listCommentParentNow(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize, "create_time desc");
        return this.listCommentParent();
    }

    // 获取列表
    @Override
    public PageData getCommentPostId(Long postId, String themeName) {
        boolean bb = cmsCommentMapper.getAllowCommentByArticleId(postId);
        if (this.commentIsSupport(themeName) && bb) {
            return PageData.getData(this.listCommentPageOne(postId));
        }
        return new PageData();
    }

    @Override
    public boolean commentIsSupport(String themeName) {
        if ("true"
                .equals(configService.selectConfigDefauleValue(OlyCommentProperties.COMMENT_CONFIG_GROUP.defaultValue(),
                        OlyCommentProperties.COMMENT_OPEN))
                && "true".equals(
                        configService.selectConfigDefauleValue(themeName, OlyWebConfigProetries.COMMENT_ENABLE))) {
            return true;
        }
        return false;
    }

}
